import { Context } from 'koa';
import { RoleService } from '../../services/system/roleService';
import { createError } from '../../middleware/errorHandler';
import { CreateRoleDto, UpdateRoleDto } from '../../dtos/system/RoleDto';
import { generateResponse } from '../../utils';

export class RoleController {
    private roleService: RoleService;

    constructor() {
        this.roleService = new RoleService();
    }

    // 获取所有角色
    getRoles = async (ctx: Context): Promise<void> => {
        try {
            let data = ctx.request.body as any
            let dics = [] as any
            if (data?.page && data?.limit) {

                dics = await this.roleService.getRolesWithPagination(data.page, data.limit);
                console.log(dics, 'dics');
            } else {
                dics = await this.roleService.getAllRoles();
            }
            generateResponse(ctx, dics);
        } catch (error: any) {
            throw createError('获取字典项列表失败', 500, error);
        }
    };

    // 根据Type获取角色
    getRoleByType = async (ctx: Context): Promise<void> => {
        try {
            const { type } = ctx.params;
            const dic = await this.roleService.getRoleByType(type);

            if (!dic) {
                throw createError('角色不存在', 404);
            }

            generateResponse(ctx, dic);
        } catch (error: any) {
            if (error.status === 404) throw error;
            throw createError('获取角色信息失败', 500, error);
        }
    };
    // 根据ID获取角色
    getRoleById = async (ctx: Context): Promise<void> => {
        try {
            const { id } = ctx.params;
            const role = await this.roleService.getRoleById(id);

            if (!role) {
                throw createError('角色不存在', 404);
            }

            generateResponse(ctx, role);
        } catch (error: any) {
            if (error.status === 404) throw error;
            throw createError('获取角色信息失败', 500, error);
        }
    };
    
    // 创建角色
    createRole = async (ctx: Context): Promise<void> => {
        try {
            // 从验证中间件获取已验证的数据
            const roleData = ctx.state.validatedData as CreateRoleDto;
            const role = await this.roleService.createRole(roleData);
            generateResponse(ctx, role, '角色创建成功');
        } catch (error: any) {

            throw createError('创建角色失败1', 400, error);
        }
    };

    // 更新角色
    updateRole = async (ctx: Context): Promise<void> => {
        try {
            const { id } = ctx.params;
            // 从验证中间件获取已验证的数据
            const roleData = ctx.state.validatedData as UpdateRoleDto;

            const role = await this.roleService.updateRole(id, roleData);

            if (!role) {
                throw createError('角色不存在', 404);
            }


            generateResponse(ctx, role, '角色更新成功');
        } catch (error: any) {
            if (error.status === 404) throw error;
            throw createError('更新角色失败', 400, error);
        }
    };

    // 删除角色
    deleteRole = async (ctx: Context): Promise<void> => {
        try {
            const { id } = ctx.params;
            const result = await this.roleService.deleteRole(id);

            if (!result) {
                throw createError('角色不存在', 404);
            }


            generateResponse(ctx, null, '角色删除成功');
        } catch (error: any) {
            if (error.status === 404) throw error;
            throw createError('删除角色失败', 500, error);
        }
    };
}